package com.healthbox.resultsservice.service.impl;

import com.healthbox.resultsservice.entity.*;
import com.healthbox.resultsservice.mapper.UserResultsMapper;
import com.healthbox.resultsservice.service.UserResultsService;
import com.healthbox.resultsservice.utils.MyTime;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserResultsServiceImpl implements UserResultsService {

    @Resource
    UserResultsMapper userResultsMapper;

    @Override
    public CommonResult readResults(String userPhone, String userRole) {
        List<TestResultForUser> origin = userResultsMapper.readTestResults(userPhone);
        List<TestResultForVue> res = new ArrayList<>();
        for (TestResultForUser item: origin) {
            TestResultForVue testResultForVue = new TestResultForVue();
            testResultForVue.setResult(item.getResult());
            testResultForVue.setAddress(item.getAddress());
            testResultForVue.setName(item.getName());
            testResultForVue.setTime(MyTime.dateTime2String(item.getTime()));
            res.add(testResultForVue);
        }
        if (res.size() >= 1) {
            return new CommonResult(200, "查看成功", res);
        } else {
            return new CommonResult(200, "暂无结果", res);
        }
    }

    @Override
    public CommonResult readNotices(Map<String, Object> map, String userPhone, String userRole) {
        Long hospital_id = Long.valueOf(map.get("hospital_id").toString());
        List<HospitalNoticeForUser> origin = userResultsMapper.readHospitalNotice(hospital_id);
        List<HospitalNoticeForVue> res = new ArrayList<>();
        for (HospitalNoticeForUser item: origin) {
            HospitalNoticeForVue hospitalNoticeForVue = new HospitalNoticeForVue();
            hospitalNoticeForVue.setContent(item.getContent());
            hospitalNoticeForVue.setTitle(item.getTitle());
            hospitalNoticeForVue.setTime(MyTime.dateTime2String(item.getTime()));
            res.add(hospitalNoticeForVue);
        }
        if (res.size() >= 1) {
            return new CommonResult(200, "查看成功", res);
        } else {
            return new CommonResult(200, "暂无公告", res);
        }
    }

    @Override
    public CommonResult readRegions(String userPhone, String userRole) {
        List<HospitalRegion> res = userResultsMapper.readHospitalRegions();
        if (res.size() >= 1) {
            return new CommonResult(200, "查看成功", res);
        } else {
            return new CommonResult(200, "暂无数据", res);
        }
    }
}
